function createXHR() {
	var xhr = null;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xhr;
}

function getWeather(time) {
	// Get Data From Hefeng Weather API By jsonp
	if (time == "tomorrow") {
		$.getJSON(
			"https://free-api.heweather.net/s6/weather/forecast?location=CN101240204&key=d694cd770e3c4788adec1d32a378f63e",
			function(data) {
				return data;
				console.log(data);
				// Rebuild Data
				/*
				let temp = Number(data['HeWeather6'][0].daily_forecast[0].tmp_max) + Number(data['HeWeather6'][0].daily_forecast[0]
					.tmp_min);
				temp = temp / 2;
				return data;
				*/
			});
	} else if (time == "now") {
		// Now Weather
		$.getJSON("https://free-api.heweather.net/s6/weather?location=CN101240204&key=d694cd770e3c4788adec1d32a378f63e",
			function(data) {
				return data;
				console.log(data);

				// Rebuild Data
				/*
				let temp = Number(data['HeWeather6'][0].daily_forecast[0].tmp_max) + Number(data['HeWeather6'][0].daily_forecast[0]
					.tmp_min);
				temp = temp / 2;
				return data;
				*/
			});
	} else {
		console.error('Error Time Argument');
	}
}

function rebuildData(data) {
	let resultData = {
		"city": data['HeWeather6'][0].basic.location,
		"temp": temp,
		"weather": data['HeWeather6'][0].daily_forecast[0].cond_txt_d + '转' + data['HeWeather6'][0].daily_forecast[0].cond_txt_n
	};

	return resultData;
}

function changeContent(data) {
	let cityContainer = document.querySelector('#cityname');
	let tempContainer = document.querySelector('#temp');
	let weatherContainer = document.querySelector('#weather');
	cityContainer.innerText = data.city;
	tempContainer.innerText = data.temp;
	weatherContainer.innerText = data.weather;
	console.log('Fresh data success!');
}

function saveTheme(themeNum) {
	// Save Theme Number to localStorage
	localStorage.clear();
	localStorage.setItem('theme', themeNum);
	if (localStorage.getItem('theme') != themeNum) {
		console.log('Exception: localStorage set error, Please check your browser.');
	} else {
		return 0;
	};
}

function getTheme() {
	// Get the Theme number from localStorage
	var themeNum = localStorage.getItem('theme');
	switch (themeNum) {
		case 0:
			return "pink"; // 哔哩粉
			break;

		case 1:
			return "cyan"; // 靓蓝青
			break;

		case 2:
			return "teal"; // 水鸭青
			break;

		case 3:
			return "blue"; // 知乎蓝
			break;

		case 4:
			return "green"; // 酷安绿
			break;

		default:
			console.log('Exception: Error Theme Selected.');
			return -1;
			break;
	}
}

function randomTheme() {
	getLocalStorage('');
	var randomNum = Math.ceil(Math.random() * 5);
	var x = document.getElementsByName('body');
	var themeNum = getTheme('');
	body.class = 'mdui-theme-primary-teal';
}

function init() {
	// Initalize Page
	var theme = getTheme();
	var x = document.getElementById('body-content');
	x.classList = "mdui-theme-primary-" + theme;
}
